import os
import requests
import time

def download_image(url, filename):
    """下载图片"""
    try:
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
        }
        response = requests.get(url, headers=headers, timeout=10)
        response.raise_for_status()
        
        with open(filename, 'wb') as f:
            f.write(response.content)
        print(f"下载成功: {filename}")
        return True
    except Exception as e:
        print(f"下载失败 {url}: {e}")
        return False

def download_missing_tulip():
    """下载缺失的粉郁金香图片"""
    image_dir = "../frontend/images/flowers"
    filename = os.path.join(image_dir, "粉郁金香.jpg")
    
    # 尝试多个郁金香图片源
    urls = [
        "https://images.unsplash.com/photo-1584464491033-06628f3a6b7b?w=800&h=600&fit=crop",
        "https://images.unsplash.com/photo-1490750967868-88aa4486c946?w=800&h=600&fit=crop",
        "https://images.unsplash.com/photo-1563241527-3004b7be0ffd?w=800&h=600&fit=crop",
        "https://picsum.photos/800/600/?random=tulip"
    ]
    
    for url in urls:
        print(f"尝试下载: {url}")
        if download_image(url, filename):
            break
        time.sleep(1)

if __name__ == "__main__":
    download_missing_tulip()